<!DOCTYPE html>

<html>
<head>
  <title>rewriter.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page_wrapper">
            <div id="jump_page">
              
                
                <a class="source" href="browser.html">
                  browser.coffee
                </a>
              
                
                <a class="source" href="cake.html">
                  cake.coffee
                </a>
              
                
                <a class="source" href="coffeescript.html">
                  coffeescript.coffee
                </a>
              
                
                <a class="source" href="command.html">
                  command.coffee
                </a>
              
                
                <a class="source" href="grammar.html">
                  grammar.coffee
                </a>
              
                
                <a class="source" href="helpers.html">
                  helpers.coffee
                </a>
              
                
                <a class="source" href="index.html">
                  index.coffee
                </a>
              
                
                <a class="source" href="lexer.html">
                  lexer.coffee
                </a>
              
                
                <a class="source" href="nodes.html">
                  nodes.coffee
                </a>
              
                
                <a class="source" href="optparse.html">
                  optparse.coffee
                </a>
              
                
                <a class="source" href="register.html">
                  register.coffee
                </a>
              
                
                <a class="source" href="repl.html">
                  repl.coffee
                </a>
              
                
                <a class="source" href="rewriter.html">
                  rewriter.coffee
                </a>
              
                
                <a class="source" href="scope.html">
                  scope.litcoffee
                </a>
              
                
                <a class="source" href="sourcemap.html">
                  sourcemap.litcoffee
                </a>
              
            </div>
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>rewriter.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-1">&#x00a7;</a>
              </div>
              <p>The CoffeeScript language has a good deal of optional syntax, implicit syntax,
and shorthand syntax. This can greatly complicate a grammar and bloat
the resulting parse table. Instead of making the parser handle it all, we take
a series of passes over the token stream, using this <strong>Rewriter</strong> to convert
shorthand into the unambiguous long form, add implicit indentation and
parentheses, and generally clean things up.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>
{throwSyntaxError, extractAllCommentTokens} = <span class="hljs-built_in">require</span> <span class="hljs-string">&#x27;./helpers&#x27;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-2">&#x00a7;</a>
              </div>
              <p>Move attached comments from one token to another.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">moveComments</span> = <span class="hljs-params">(fromToken, toToken)</span> -&gt;</span>
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> fromToken.comments
  <span class="hljs-keyword">if</span> toToken.comments <span class="hljs-keyword">and</span> toToken.comments.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
    unshiftedComments = []
    <span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> fromToken.comments
      <span class="hljs-keyword">if</span> comment.unshift
        unshiftedComments.push comment
      <span class="hljs-keyword">else</span>
        toToken.comments.push comment
    toToken.comments = unshiftedComments.concat toToken.comments
  <span class="hljs-keyword">else</span>
    toToken.comments = fromToken.comments
  <span class="hljs-keyword">delete</span> fromToken.comments</pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-3">&#x00a7;</a>
              </div>
              <p>Create a generated token: one that exists due to a use of implicit syntax.
Optionally have this new token take the attached comments from another token.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-title">generate</span> = <span class="hljs-params">(tag, value, origin, commentsToken)</span> -&gt;</span>
  token = [tag, value]
  token.generated = <span class="hljs-literal">yes</span>
  token.origin = origin <span class="hljs-keyword">if</span> origin
  moveComments commentsToken, token <span class="hljs-keyword">if</span> commentsToken
  token</pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-4">&#x00a7;</a>
              </div>
              <p>The <strong>Rewriter</strong> class is used by the <a href="lexer.html">Lexer</a>, directly against
its internal array of tokens.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.Rewriter = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Rewriter</span></span></pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-5">&#x00a7;</a>
              </div>
              <p>Rewrite the token stream in multiple passes, one logical filter at
a time. This could certainly be changed into a single pass through the
stream, with a big ol’ efficient switch, but it’s much nicer to work with
like this. The order of these passes matters—indentation must be
corrected before implicit parentheses can be wrapped around blocks of code.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  rewrite: <span class="hljs-function"><span class="hljs-params">(@tokens)</span> -&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-6">&#x00a7;</a>
              </div>
              <p>Set environment variable <code>DEBUG_TOKEN_STREAM</code> to <code>true</code> to output token
debugging info. Also set <code>DEBUG_REWRITTEN_TOKEN_STREAM</code> to <code>true</code> to
output the token stream after it has been rewritten by this file.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-keyword">if</span> process?.env?.DEBUG_TOKEN_STREAM
      console.log <span class="hljs-string">&#x27;Initial token stream:&#x27;</span> <span class="hljs-keyword">if</span> process.env.DEBUG_REWRITTEN_TOKEN_STREAM
      console.log (t[<span class="hljs-number">0</span>] + <span class="hljs-string">&#x27;/&#x27;</span> + t[<span class="hljs-number">1</span>] + (<span class="hljs-keyword">if</span> t.comments <span class="hljs-keyword">then</span> <span class="hljs-string">&#x27;*&#x27;</span> <span class="hljs-keyword">else</span> <span class="hljs-string">&#x27;&#x27;</span>) <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> @tokens).join <span class="hljs-string">&#x27; &#x27;</span>
    @removeLeadingNewlines()
    @closeOpenCalls()
    @closeOpenIndexes()
    @normalizeLines()
    @tagPostfixConditionals()
    @addImplicitBracesAndParens()
    @rescueStowawayComments()
    @addLocationDataToGeneratedTokens()
    @enforceValidJSXAttributes()
    @fixIndentationLocationData()
    @exposeTokenDataToGrammar()
    <span class="hljs-keyword">if</span> process?.env?.DEBUG_REWRITTEN_TOKEN_STREAM
      console.log <span class="hljs-string">&#x27;Rewritten token stream:&#x27;</span> <span class="hljs-keyword">if</span> process.env.DEBUG_TOKEN_STREAM
      console.log (t[<span class="hljs-number">0</span>] + <span class="hljs-string">&#x27;/&#x27;</span> + t[<span class="hljs-number">1</span>] + (<span class="hljs-keyword">if</span> t.comments <span class="hljs-keyword">then</span> <span class="hljs-string">&#x27;*&#x27;</span> <span class="hljs-keyword">else</span> <span class="hljs-string">&#x27;&#x27;</span>) <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> @tokens).join <span class="hljs-string">&#x27; &#x27;</span>
    @tokens</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-7">&#x00a7;</a>
              </div>
              <p>Rewrite the token stream, looking one token ahead and behind.
Allow the return value of the block to tell us how many tokens to move
forwards (or backwards) in the stream, to make sure we don’t miss anything
as tokens are inserted and removed, and the stream changes length under
our feet.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  scanTokens: <span class="hljs-function"><span class="hljs-params">(block)</span> -&gt;</span>
    {tokens} = this
    i = <span class="hljs-number">0</span>
    i += block.call this, token, i, tokens <span class="hljs-keyword">while</span> token = tokens[i]
    <span class="hljs-literal">true</span>

  detectEnd: <span class="hljs-function"><span class="hljs-params">(i, condition, action, opts = {})</span> -&gt;</span>
    {tokens} = this
    levels = <span class="hljs-number">0</span>
    <span class="hljs-keyword">while</span> token = tokens[i]
      <span class="hljs-keyword">return</span> action.call this, token, i <span class="hljs-keyword">if</span> levels <span class="hljs-keyword">is</span> <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> condition.call this, token, i
      <span class="hljs-keyword">if</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> EXPRESSION_START
        levels += <span class="hljs-number">1</span>
      <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> EXPRESSION_END
        levels -= <span class="hljs-number">1</span>
      <span class="hljs-keyword">if</span> levels &lt; <span class="hljs-number">0</span>
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> opts.returnOnNegativeLevel
        <span class="hljs-keyword">return</span> action.call this, token, i
      i += <span class="hljs-number">1</span>
    i - <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-8">&#x00a7;</a>
              </div>
              <p>Leading newlines would introduce an ambiguity in the grammar, so we
dispatch them here.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  removeLeadingNewlines: <span class="hljs-function">-&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-9">&#x00a7;</a>
              </div>
              <p>Find the index of the first non-<code>TERMINATOR</code> token.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-keyword">break</span> <span class="hljs-keyword">for</span> [tag], i <span class="hljs-keyword">in</span> @tokens <span class="hljs-keyword">when</span> tag <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> i <span class="hljs-keyword">is</span> <span class="hljs-number">0</span></pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-10">&#x00a7;</a>
              </div>
              <p>If there are any comments attached to the tokens we’re about to discard,
shift them forward to what will become the new first token.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="hljs-keyword">for</span> leadingNewlineToken <span class="hljs-keyword">in</span> @tokens[<span class="hljs-number">0.</span>..i]
      moveComments leadingNewlineToken, @tokens[i]</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-11">&#x00a7;</a>
              </div>
              <p>Discard all the leading newline tokens.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    @tokens.splice <span class="hljs-number">0</span>, i</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-12">&#x00a7;</a>
              </div>
              <p>The lexer has tagged the opening parenthesis of a method call. Match it with
its paired close.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  closeOpenCalls: <span class="hljs-function">-&gt;</span>
<span class="hljs-function">    <span class="hljs-title">condition</span> = <span class="hljs-params">(token, i)</span> -&gt;</span>
      token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;)&#x27;</span>, <span class="hljs-string">&#x27;CALL_END&#x27;</span>]
<span class="hljs-function">
    <span class="hljs-title">action</span> = <span class="hljs-params">(token, i)</span> -&gt;</span>
      token[<span class="hljs-number">0</span>] = <span class="hljs-string">&#x27;CALL_END&#x27;</span>

    @scanTokens (token, i) -&gt;
      @detectEnd i + <span class="hljs-number">1</span>, condition, action <span class="hljs-keyword">if</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;CALL_START&#x27;</span>
      <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-13">&#x00a7;</a>
              </div>
              <p>The lexer has tagged the opening bracket of an indexing operation call.
Match it with its paired close.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  closeOpenIndexes: <span class="hljs-function">-&gt;</span>
    startToken = <span class="hljs-literal">null</span>
<span class="hljs-function">    <span class="hljs-title">condition</span> = <span class="hljs-params">(token, i)</span> -&gt;</span>
      token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;]&#x27;</span>, <span class="hljs-string">&#x27;INDEX_END&#x27;</span>]
<span class="hljs-function">
    <span class="hljs-title">action</span> = <span class="hljs-params">(token, i)</span> -&gt;</span>
      <span class="hljs-keyword">if</span> @tokens.length &gt;= i <span class="hljs-keyword">and</span> @tokens[i + <span class="hljs-number">1</span>][<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;:&#x27;</span>
        startToken[<span class="hljs-number">0</span>] = <span class="hljs-string">&#x27;[&#x27;</span>
        token[<span class="hljs-number">0</span>] = <span class="hljs-string">&#x27;]&#x27;</span>
      <span class="hljs-keyword">else</span>
        token[<span class="hljs-number">0</span>] = <span class="hljs-string">&#x27;INDEX_END&#x27;</span>

    @scanTokens (token, i) -&gt;
      <span class="hljs-keyword">if</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;INDEX_START&#x27;</span>
        startToken = token
        @detectEnd i + <span class="hljs-number">1</span>, condition, action
      <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-14">&#x00a7;</a>
              </div>
              <p>Match tags in token stream starting at <code>i</code> with <code>pattern</code>.
<code>pattern</code> may consist of strings (equality), an array of strings (one of)
or null (wildcard). Returns the index of the match or -1 if no match.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  indexOfTag: <span class="hljs-function"><span class="hljs-params">(i, pattern...)</span> -&gt;</span>
    fuzz = <span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> [<span class="hljs-number">0</span> ... pattern.length]
      <span class="hljs-keyword">continue</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> pattern[j]?
      pattern[j] = [pattern[j]] <span class="hljs-keyword">if</span> <span class="hljs-keyword">typeof</span> pattern[j] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;string&#x27;</span>
      <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span> <span class="hljs-keyword">if</span> @tag(i + j + fuzz) <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> pattern[j]
    i + j + fuzz - <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-15">&#x00a7;</a>
              </div>
              <p>Returns <code>yes</code> if standing in front of something looking like
<code>@&lt;x&gt;:</code>, <code>&lt;x&gt;:</code> or <code>&lt;EXPRESSION_START&gt;&lt;x&gt;...&lt;EXPRESSION_END&gt;:</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  looksObjectish: <span class="hljs-function"><span class="hljs-params">(j)</span> -&gt;</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @indexOfTag(j, <span class="hljs-string">&#x27;@&#x27;</span>, <span class="hljs-literal">null</span>, <span class="hljs-string">&#x27;:&#x27;</span>) <span class="hljs-keyword">isnt</span> <span class="hljs-number">-1</span> <span class="hljs-keyword">or</span> @indexOfTag(j, <span class="hljs-literal">null</span>, <span class="hljs-string">&#x27;:&#x27;</span>) <span class="hljs-keyword">isnt</span> <span class="hljs-number">-1</span>
    index = @indexOfTag j, EXPRESSION_START
    <span class="hljs-keyword">if</span> index <span class="hljs-keyword">isnt</span> <span class="hljs-number">-1</span>
      end = <span class="hljs-literal">null</span>
      @detectEnd index + <span class="hljs-number">1</span>, <span class="hljs-function">(<span class="hljs-params">(token)</span> -&gt;</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> EXPRESSION_END), <span class="hljs-function">(<span class="hljs-params">(token, i)</span> -&gt;</span> end = i)
      <span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> @tag(end + <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;:&#x27;</span>
    <span class="hljs-literal">no</span></pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-16">&#x00a7;</a>
              </div>
              <p>Returns <code>yes</code> if current line of tokens contain an element of tags on same
expression level. Stop searching at <code>LINEBREAKS</code> or explicit start of
containing balanced expression.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  findTagsBackwards: <span class="hljs-function"><span class="hljs-params">(i, tags)</span> -&gt;</span>
    backStack = []
    <span class="hljs-keyword">while</span> i &gt;= <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> (backStack.length <span class="hljs-keyword">or</span>
          @tag(i) <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> tags <span class="hljs-keyword">and</span>
          (@tag(i) <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> EXPRESSION_START <span class="hljs-keyword">or</span> @tokens[i].generated) <span class="hljs-keyword">and</span>
          @tag(i) <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> LINEBREAKS)
      backStack.push @tag(i) <span class="hljs-keyword">if</span> @tag(i) <span class="hljs-keyword">in</span> EXPRESSION_END
      backStack.pop() <span class="hljs-keyword">if</span> @tag(i) <span class="hljs-keyword">in</span> EXPRESSION_START <span class="hljs-keyword">and</span> backStack.length
      i -= <span class="hljs-number">1</span>
    @tag(i) <span class="hljs-keyword">in</span> tags</pre></div></div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-17">&#x00a7;</a>
              </div>
              <p>Look for signs of implicit calls and objects in the token stream and
add them.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  addImplicitBracesAndParens: <span class="hljs-function">-&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-18">&#x00a7;</a>
              </div>
              <p>Track current balancing depth (both implicit and explicit) on stack.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    stack = []
    start = <span class="hljs-literal">null</span>

    @scanTokens (token, i, tokens) -&gt;
      [tag]     = token
      [prevTag] = prevToken = <span class="hljs-keyword">if</span> i &gt; <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> tokens[i - <span class="hljs-number">1</span>] <span class="hljs-keyword">else</span> []
      [nextTag] = nextToken = <span class="hljs-keyword">if</span> i &lt; tokens.length - <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> tokens[i + <span class="hljs-number">1</span>] <span class="hljs-keyword">else</span> []
<span class="hljs-function">      <span class="hljs-title">stackTop</span>  = -&gt;</span> stack[stack.length - <span class="hljs-number">1</span>]
      startIdx  = i</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-19">&#x00a7;</a>
              </div>
              <p>Helper function, used for keeping track of the number of tokens consumed
and spliced, when returning for getting a new token.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-function">      <span class="hljs-title">forward</span>   = <span class="hljs-params">(n)</span> -&gt;</span> i - startIdx + n</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-20">&#x00a7;</a>
              </div>
              <p>Helper functions</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-function">      <span class="hljs-title">isImplicit</span>        = <span class="hljs-params">(stackItem)</span> -&gt;</span> stackItem?[<span class="hljs-number">2</span>]?.ours
<span class="hljs-function">      <span class="hljs-title">isImplicitObject</span>  = <span class="hljs-params">(stackItem)</span> -&gt;</span> isImplicit(stackItem) <span class="hljs-keyword">and</span> stackItem?[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;{&#x27;</span>
<span class="hljs-function">      <span class="hljs-title">isImplicitCall</span>    = <span class="hljs-params">(stackItem)</span> -&gt;</span> isImplicit(stackItem) <span class="hljs-keyword">and</span> stackItem?[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;(&#x27;</span>
<span class="hljs-function">      <span class="hljs-title">inImplicit</span>        = -&gt;</span> isImplicit stackTop()
<span class="hljs-function">      <span class="hljs-title">inImplicitCall</span>    = -&gt;</span> isImplicitCall stackTop()
<span class="hljs-function">      <span class="hljs-title">inImplicitObject</span>  = -&gt;</span> isImplicitObject stackTop()</pre></div></div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-21">&#x00a7;</a>
              </div>
              <p>Unclosed control statement inside implicit parens (like
class declaration or if-conditionals).</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-function">      <span class="hljs-title">inImplicitControl</span> = -&gt;</span> inImplicit() <span class="hljs-keyword">and</span> stackTop()?[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;CONTROL&#x27;</span>
<span class="hljs-function">
      <span class="hljs-title">startImplicitCall</span> = <span class="hljs-params">(idx)</span> -&gt;</span>
        stack.push [<span class="hljs-string">&#x27;(&#x27;</span>, idx, ours: <span class="hljs-literal">yes</span>]
        tokens.splice idx, <span class="hljs-number">0</span>, generate <span class="hljs-string">&#x27;CALL_START&#x27;</span>, <span class="hljs-string">&#x27;(&#x27;</span>, [<span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-string">&#x27;implicit function call&#x27;</span>, token[<span class="hljs-number">2</span>]], prevToken
<span class="hljs-function">
      <span class="hljs-title">endImplicitCall</span> = -&gt;</span>
        stack.pop()
        tokens.splice i, <span class="hljs-number">0</span>, generate <span class="hljs-string">&#x27;CALL_END&#x27;</span>, <span class="hljs-string">&#x27;)&#x27;</span>, [<span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-string">&#x27;end of input&#x27;</span>, token[<span class="hljs-number">2</span>]], prevToken
        i += <span class="hljs-number">1</span>
<span class="hljs-function">
      <span class="hljs-title">startImplicitObject</span> = <span class="hljs-params">(idx, {startsLine = <span class="hljs-literal">yes</span>, continuationLineIndent} = {})</span> -&gt;</span>
        stack.push [<span class="hljs-string">&#x27;{&#x27;</span>, idx, sameLine: <span class="hljs-literal">yes</span>, startsLine: startsLine, ours: <span class="hljs-literal">yes</span>, continuationLineIndent: continuationLineIndent]
        val = <span class="hljs-keyword">new</span> <span class="hljs-built_in">String</span> <span class="hljs-string">&#x27;{&#x27;</span>
        val.generated = <span class="hljs-literal">yes</span>
        tokens.splice idx, <span class="hljs-number">0</span>, generate <span class="hljs-string">&#x27;{&#x27;</span>, val, token, prevToken
<span class="hljs-function">
      <span class="hljs-title">endImplicitObject</span> = <span class="hljs-params">(j)</span> -&gt;</span>
        j = j ? i
        stack.pop()
        tokens.splice j, <span class="hljs-number">0</span>, generate <span class="hljs-string">&#x27;}&#x27;</span>, <span class="hljs-string">&#x27;}&#x27;</span>, token, prevToken
        i += <span class="hljs-number">1</span>
<span class="hljs-function">
      <span class="hljs-title">implicitObjectContinues</span> = <span class="hljs-params">(j)</span> =&gt;</span>
        nextTerminatorIdx = <span class="hljs-literal">null</span>
        @detectEnd j,
          <span class="hljs-function"><span class="hljs-params">(token)</span> -&gt;</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>
          (token, i) -&gt; nextTerminatorIdx = i
          returnOnNegativeLevel: <span class="hljs-literal">yes</span>
        <span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> nextTerminatorIdx?
        @looksObjectish nextTerminatorIdx + <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-22">&#x00a7;</a>
              </div>
              <p>Don’t end an implicit call/object on next indent if any of these are in an argument/value.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> (
        (inImplicitCall() <span class="hljs-keyword">or</span> inImplicitObject()) <span class="hljs-keyword">and</span> tag <span class="hljs-keyword">in</span> CONTROL_IN_IMPLICIT <span class="hljs-keyword">or</span>
        inImplicitObject() <span class="hljs-keyword">and</span> prevTag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;:&#x27;</span> <span class="hljs-keyword">and</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;FOR&#x27;</span>
      )
        stack.push [<span class="hljs-string">&#x27;CONTROL&#x27;</span>, i, ours: <span class="hljs-literal">yes</span>]
        <span class="hljs-keyword">return</span> forward(<span class="hljs-number">1</span>)

      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;INDENT&#x27;</span> <span class="hljs-keyword">and</span> inImplicit()</pre></div></div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-23">&#x00a7;</a>
              </div>
              <p>An <code>INDENT</code> closes an implicit call unless</p>
<ol>
<li>We have seen a <code>CONTROL</code> argument on the line.</li>
<li>The last token before the indent is part of the list below.</li>
</ol>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-keyword">if</span> prevTag <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;=&gt;&#x27;</span>, <span class="hljs-string">&#x27;-&gt;&#x27;</span>, <span class="hljs-string">&#x27;[&#x27;</span>, <span class="hljs-string">&#x27;(&#x27;</span>, <span class="hljs-string">&#x27;,&#x27;</span>, <span class="hljs-string">&#x27;{&#x27;</span>, <span class="hljs-string">&#x27;ELSE&#x27;</span>, <span class="hljs-string">&#x27;=&#x27;</span>]
          <span class="hljs-keyword">while</span> inImplicitCall() <span class="hljs-keyword">or</span> inImplicitObject() <span class="hljs-keyword">and</span> prevTag <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;:&#x27;</span>
            <span class="hljs-keyword">if</span> inImplicitCall()
              endImplicitCall()
            <span class="hljs-keyword">else</span>
              endImplicitObject()
        stack.pop() <span class="hljs-keyword">if</span> inImplicitControl()
        stack.push [tag, i]
        <span class="hljs-keyword">return</span> forward(<span class="hljs-number">1</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-24">&#x00a7;</a>
              </div>
              <p>Straightforward start of explicit expression.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">in</span> EXPRESSION_START
        stack.push [tag, i]
        <span class="hljs-keyword">return</span> forward(<span class="hljs-number">1</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-25">&#x00a7;</a>
              </div>
              <p>Close all implicit expressions inside of explicitly closed expressions.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">in</span> EXPRESSION_END
        <span class="hljs-keyword">while</span> inImplicit()
          <span class="hljs-keyword">if</span> inImplicitCall()
            endImplicitCall()
          <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> inImplicitObject()
            endImplicitObject()
          <span class="hljs-keyword">else</span>
            stack.pop()
        start = stack.pop()
<span class="hljs-function">
      <span class="hljs-title">inControlFlow</span> = =&gt;</span>
        seenFor = @findTagsBackwards(i, [<span class="hljs-string">&#x27;FOR&#x27;</span>]) <span class="hljs-keyword">and</span> @findTagsBackwards(i, [<span class="hljs-string">&#x27;FORIN&#x27;</span>, <span class="hljs-string">&#x27;FOROF&#x27;</span>, <span class="hljs-string">&#x27;FORFROM&#x27;</span>])
        controlFlow = seenFor <span class="hljs-keyword">or</span> @findTagsBackwards i, [<span class="hljs-string">&#x27;WHILE&#x27;</span>, <span class="hljs-string">&#x27;UNTIL&#x27;</span>, <span class="hljs-string">&#x27;LOOP&#x27;</span>, <span class="hljs-string">&#x27;LEADING_WHEN&#x27;</span>]
        <span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> controlFlow
        isFunc = <span class="hljs-literal">no</span>
        tagCurrentLine = token[<span class="hljs-number">2</span>].first_line
        @detectEnd i,
          <span class="hljs-function"><span class="hljs-params">(token, i)</span> -&gt;</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> LINEBREAKS
          (token, i) -&gt;
            [prevTag, ,{first_line}] = tokens[i - <span class="hljs-number">1</span>] || []
            isFunc = tagCurrentLine <span class="hljs-keyword">is</span> first_line <span class="hljs-keyword">and</span> prevTag <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;-&gt;&#x27;</span>, <span class="hljs-string">&#x27;=&gt;&#x27;</span>]
          returnOnNegativeLevel: <span class="hljs-literal">yes</span>
        isFunc</pre></div></div>
            
        </li>
        
        
        <li id="section-26">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-26">&#x00a7;</a>
              </div>
              <p>Recognize standard implicit calls like
f a, f() b, f? c, h[0] d etc.
Added support for spread dots on the left side: f …a</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> (tag <span class="hljs-keyword">in</span> IMPLICIT_FUNC <span class="hljs-keyword">and</span> token.spaced <span class="hljs-keyword">or</span>
          tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;?&#x27;</span> <span class="hljs-keyword">and</span> i &gt; <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> tokens[i - <span class="hljs-number">1</span>].spaced) <span class="hljs-keyword">and</span>
         (nextTag <span class="hljs-keyword">in</span> IMPLICIT_CALL <span class="hljs-keyword">or</span>
         (nextTag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;...&#x27;</span> <span class="hljs-keyword">and</span> @tag(i + <span class="hljs-number">2</span>) <span class="hljs-keyword">in</span> IMPLICIT_CALL <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @findTagsBackwards(i, [<span class="hljs-string">&#x27;INDEX_START&#x27;</span>, <span class="hljs-string">&#x27;[&#x27;</span>])) <span class="hljs-keyword">or</span>
          nextTag <span class="hljs-keyword">in</span> IMPLICIT_UNSPACED_CALL <span class="hljs-keyword">and</span>
          <span class="hljs-keyword">not</span> nextToken.spaced <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> nextToken.newLine) <span class="hljs-keyword">and</span>
          <span class="hljs-keyword">not</span> inControlFlow()
        tag = token[<span class="hljs-number">0</span>] = <span class="hljs-string">&#x27;FUNC_EXIST&#x27;</span> <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;?&#x27;</span>
        startImplicitCall i + <span class="hljs-number">1</span>
        <span class="hljs-keyword">return</span> forward(<span class="hljs-number">2</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-27">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-27">&#x00a7;</a>
              </div>
              <p>Implicit call taking an implicit indented object as first argument.</p>
<pre><code>f
  a: b
  c: d
</code></pre>
<p>Don’t accept implicit calls of this type, when on the same line
as the control structures below as that may misinterpret constructs like:</p>
<pre><code><span class="hljs-keyword">if</span> f
   a: <span class="hljs-number">1</span>
</code></pre>
<p>as</p>
<pre><code><span class="hljs-keyword">if</span> f(a: <span class="hljs-number">1</span>)
</code></pre>
<p>which is probably always unintended.
Furthermore don’t allow this in the first line of a literal array
or explicit object, as that creates grammatical ambiguities (#5368).</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">in</span> IMPLICIT_FUNC <span class="hljs-keyword">and</span>
         @indexOfTag(i + <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;INDENT&#x27;</span>) &gt; <span class="hljs-number">-1</span> <span class="hljs-keyword">and</span> @looksObjectish(i + <span class="hljs-number">2</span>) <span class="hljs-keyword">and</span>
         <span class="hljs-keyword">not</span> @findTagsBackwards(i, [<span class="hljs-string">&#x27;CLASS&#x27;</span>, <span class="hljs-string">&#x27;EXTENDS&#x27;</span>, <span class="hljs-string">&#x27;IF&#x27;</span>, <span class="hljs-string">&#x27;CATCH&#x27;</span>,
          <span class="hljs-string">&#x27;SWITCH&#x27;</span>, <span class="hljs-string">&#x27;LEADING_WHEN&#x27;</span>, <span class="hljs-string">&#x27;FOR&#x27;</span>, <span class="hljs-string">&#x27;WHILE&#x27;</span>, <span class="hljs-string">&#x27;UNTIL&#x27;</span>]) <span class="hljs-keyword">and</span>
         <span class="hljs-keyword">not</span> ((s = stackTop()?[<span class="hljs-number">0</span>]) <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;{&#x27;</span>, <span class="hljs-string">&#x27;[&#x27;</span>] <span class="hljs-keyword">and</span>
              <span class="hljs-keyword">not</span> isImplicit(stackTop()) <span class="hljs-keyword">and</span>
              @findTagsBackwards(i, s))
        startImplicitCall i + <span class="hljs-number">1</span>
        stack.push [<span class="hljs-string">&#x27;INDENT&#x27;</span>, i + <span class="hljs-number">2</span>]
        <span class="hljs-keyword">return</span> forward(<span class="hljs-number">3</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-28">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-28">&#x00a7;</a>
              </div>
              <p>Implicit objects start here.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;:&#x27;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-29">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-29">&#x00a7;</a>
              </div>
              <p>Go back to the (implicit) start of the object.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        s = <span class="hljs-keyword">switch</span>
          <span class="hljs-keyword">when</span> @tag(i - <span class="hljs-number">1</span>) <span class="hljs-keyword">in</span> EXPRESSION_END
            [startTag, startIndex] = start
            <span class="hljs-keyword">if</span> startTag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;[&#x27;</span> <span class="hljs-keyword">and</span> startIndex &gt; <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> @tag(startIndex - <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;@&#x27;</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> tokens[startIndex - <span class="hljs-number">1</span>].spaced
              startIndex - <span class="hljs-number">1</span>
            <span class="hljs-keyword">else</span>
              startIndex
          <span class="hljs-keyword">when</span> @tag(i - <span class="hljs-number">2</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;@&#x27;</span> <span class="hljs-keyword">then</span> i - <span class="hljs-number">2</span>
          <span class="hljs-keyword">else</span> i - <span class="hljs-number">1</span>

        startsLine = s &lt;= <span class="hljs-number">0</span> <span class="hljs-keyword">or</span> @tag(s - <span class="hljs-number">1</span>) <span class="hljs-keyword">in</span> LINEBREAKS <span class="hljs-keyword">or</span> tokens[s - <span class="hljs-number">1</span>].newLine</pre></div></div>
            
        </li>
        
        
        <li id="section-30">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-30">&#x00a7;</a>
              </div>
              <p>Are we just continuing an already declared object?
Including the case where we indent on the line after an explicit ‘{‘.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-keyword">if</span> stackTop()
          [stackTag, stackIdx] = stackTop()
          stackNext = stack[stack.length - <span class="hljs-number">2</span>]
          <span class="hljs-keyword">if</span> (stackTag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;{&#x27;</span> <span class="hljs-keyword">or</span>
              stackTag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;INDENT&#x27;</span> <span class="hljs-keyword">and</span> stackNext?[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;{&#x27;</span> <span class="hljs-keyword">and</span>
              <span class="hljs-keyword">not</span> isImplicit(stackNext) <span class="hljs-keyword">and</span>
              @findTagsBackwards(stackIdx<span class="hljs-number">-1</span>, [<span class="hljs-string">&#x27;{&#x27;</span>])) <span class="hljs-keyword">and</span>
             (startsLine <span class="hljs-keyword">or</span> @tag(s - <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;,&#x27;</span> <span class="hljs-keyword">or</span> @tag(s - <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;{&#x27;</span>) <span class="hljs-keyword">and</span>
             @tag(s - <span class="hljs-number">1</span>) <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> UNFINISHED
            <span class="hljs-keyword">return</span> forward(<span class="hljs-number">1</span>)

        preObjectToken = <span class="hljs-keyword">if</span> i &gt; <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> tokens[i - <span class="hljs-number">2</span>] <span class="hljs-keyword">else</span> []
        startImplicitObject(s, {startsLine: !!startsLine, continuationLineIndent: preObjectToken.continuationLineIndent})
        <span class="hljs-keyword">return</span> forward(<span class="hljs-number">2</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-31">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-31">&#x00a7;</a>
              </div>
              <p>End implicit calls when chaining method calls
like e.g.:</p>
<pre><code>f -&gt;
  a
.g b, <span class="hljs-function">-&gt;</span>
  c
.h a
</code></pre>
<p>and also</p>
<pre><code>f a
.g b
.h a
</code></pre>

            </div>
            
        </li>
        
        
        <li id="section-32">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-32">&#x00a7;</a>
              </div>
              <p>Mark all enclosing objects as not sameLine</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">in</span> LINEBREAKS
        <span class="hljs-keyword">for</span> stackItem <span class="hljs-keyword">in</span> stack <span class="hljs-keyword">by</span> <span class="hljs-number">-1</span>
          <span class="hljs-keyword">break</span> <span class="hljs-keyword">unless</span> isImplicit stackItem
          stackItem[<span class="hljs-number">2</span>].sameLine = <span class="hljs-literal">no</span> <span class="hljs-keyword">if</span> isImplicitObject stackItem</pre></div></div>
            
        </li>
        
        
        <li id="section-33">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-33">&#x00a7;</a>
              </div>
              <p>End indented-continuation-line implicit objects once that indentation is over.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span> <span class="hljs-keyword">and</span> token.endsContinuationLineIndentation
        {preContinuationLineIndent} = token.endsContinuationLineIndentation
        <span class="hljs-keyword">while</span> inImplicitObject() <span class="hljs-keyword">and</span> (implicitObjectIndent = stackTop()[<span class="hljs-number">2</span>].continuationLineIndent)? <span class="hljs-keyword">and</span> implicitObjectIndent &gt; preContinuationLineIndent
          endImplicitObject()

      newLine = prevTag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;OUTDENT&#x27;</span> <span class="hljs-keyword">or</span> prevToken.newLine
      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">in</span> IMPLICIT_END <span class="hljs-keyword">or</span>
          (tag <span class="hljs-keyword">in</span> CALL_CLOSERS <span class="hljs-keyword">and</span> newLine) <span class="hljs-keyword">or</span>
          (tag <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;..&#x27;</span>, <span class="hljs-string">&#x27;...&#x27;</span>] <span class="hljs-keyword">and</span> @findTagsBackwards(i, [<span class="hljs-string">&quot;INDEX_START&quot;</span>]))
        <span class="hljs-keyword">while</span> inImplicit()
          [stackTag, stackIdx, {sameLine, startsLine}] = stackTop()</pre></div></div>
            
        </li>
        
        
        <li id="section-34">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-34">&#x00a7;</a>
              </div>
              <p>Close implicit calls when reached end of argument list</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">if</span> inImplicitCall() <span class="hljs-keyword">and</span> prevTag <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;,&#x27;</span> <span class="hljs-keyword">or</span>
              (prevTag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;,&#x27;</span> <span class="hljs-keyword">and</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> nextTag?)
            endImplicitCall()</pre></div></div>
            
        </li>
        
        
        <li id="section-35">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-35">&#x00a7;</a>
              </div>
              <p>Close implicit objects such as:
return a: 1, b: 2 unless true</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> inImplicitObject() <span class="hljs-keyword">and</span> sameLine <span class="hljs-keyword">and</span>
                  tag <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span> <span class="hljs-keyword">and</span> prevTag <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;:&#x27;</span> <span class="hljs-keyword">and</span>
                  <span class="hljs-keyword">not</span> (tag <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;POST_IF&#x27;</span>, <span class="hljs-string">&#x27;FOR&#x27;</span>, <span class="hljs-string">&#x27;WHILE&#x27;</span>, <span class="hljs-string">&#x27;UNTIL&#x27;</span>] <span class="hljs-keyword">and</span> startsLine <span class="hljs-keyword">and</span> implicitObjectContinues(i + <span class="hljs-number">1</span>))
            endImplicitObject()</pre></div></div>
            
        </li>
        
        
        <li id="section-36">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-36">&#x00a7;</a>
              </div>
              <p>Close implicit objects when at end of line, line didn’t end with a comma
and the implicit object didn’t start the line or the next line doesn’t look like
the continuation of an object.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>          <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> inImplicitObject() <span class="hljs-keyword">and</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span> <span class="hljs-keyword">and</span> prevTag <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;,&#x27;</span> <span class="hljs-keyword">and</span>
                  <span class="hljs-keyword">not</span> (startsLine <span class="hljs-keyword">and</span> @looksObjectish(i + <span class="hljs-number">1</span>))
            endImplicitObject()
          <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> inImplicitControl() <span class="hljs-keyword">and</span> tokens[stackTop()[<span class="hljs-number">1</span>]][<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;CLASS&#x27;</span> <span class="hljs-keyword">and</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>
            stack.pop()
          <span class="hljs-keyword">else</span>
            <span class="hljs-keyword">break</span></pre></div></div>
            
        </li>
        
        
        <li id="section-37">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-37">&#x00a7;</a>
              </div>
              <p>Close implicit object if comma is the last character
and what comes after doesn’t look like it belongs.
This is used for trailing commas and calls, like:</p>
<pre><code>x =
    a: b,
    c: d,
e = <span class="hljs-number">2</span>
</code></pre>
<p>and</p>
<pre><code>f a, b: c, d: e, f, g: h: i, j
</code></pre>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;,&#x27;</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> @looksObjectish(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">and</span> inImplicitObject() <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> (@tag(i + <span class="hljs-number">2</span>) <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;FOROF&#x27;</span>, <span class="hljs-string">&#x27;FORIN&#x27;</span>]) <span class="hljs-keyword">and</span>
         (nextTag <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> @looksObjectish(i + <span class="hljs-number">2</span>))</pre></div></div>
            
        </li>
        
        
        <li id="section-38">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-38">&#x00a7;</a>
              </div>
              <p>When nextTag is OUTDENT the comma is insignificant and
should just be ignored so embed it in the implicit object.</p>
<p>When it isn’t the comma go on to play a role in a call or
array further up the stack, so give it a chance.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        offset = <span class="hljs-keyword">if</span> nextTag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;OUTDENT&#x27;</span> <span class="hljs-keyword">then</span> <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>
        <span class="hljs-keyword">while</span> inImplicitObject()
          endImplicitObject i + offset
      <span class="hljs-keyword">return</span> forward(<span class="hljs-number">1</span>)</pre></div></div>
            
        </li>
        
        
        <li id="section-39">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-39">&#x00a7;</a>
              </div>
              <p>Make sure only strings and wrapped expressions are used in JSX attributes.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  enforceValidJSXAttributes: <span class="hljs-function">-&gt;</span>
    @scanTokens (token, i, tokens) -&gt;
      <span class="hljs-keyword">if</span> token.jsxColon
        next = tokens[i + <span class="hljs-number">1</span>]
        <span class="hljs-keyword">if</span> next[<span class="hljs-number">0</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;STRING_START&#x27;</span>, <span class="hljs-string">&#x27;STRING&#x27;</span>, <span class="hljs-string">&#x27;(&#x27;</span>]
          throwSyntaxError <span class="hljs-string">&#x27;expected wrapped or quoted JSX attribute&#x27;</span>, next[<span class="hljs-number">2</span>]
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-40">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-40">&#x00a7;</a>
              </div>
              <p>Not all tokens survive processing by the parser. To avoid comments getting
lost into the ether, find comments attached to doomed tokens and move them
to a token that will make it to the other side.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  rescueStowawayComments: <span class="hljs-function">-&gt;</span>
<span class="hljs-function">    <span class="hljs-title">insertPlaceholder</span> = <span class="hljs-params">(token, j, tokens, method)</span> -&gt;</span>
      tokens[method] generate <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>, <span class="hljs-string">&#x27;\n&#x27;</span>, tokens[j] <span class="hljs-keyword">unless</span> tokens[j][<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>
      tokens[method] generate <span class="hljs-string">&#x27;JS&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>, tokens[j], token
<span class="hljs-function">
    <span class="hljs-title">dontShiftForward</span> = <span class="hljs-params">(i, tokens)</span> -&gt;</span>
      j = i + <span class="hljs-number">1</span>
      <span class="hljs-keyword">while</span> j <span class="hljs-keyword">isnt</span> tokens.length <span class="hljs-keyword">and</span> tokens[j][<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> DISCARDED
        <span class="hljs-keyword">return</span> <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> tokens[j][<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;INTERPOLATION_END&#x27;</span>
        j++
      <span class="hljs-literal">no</span>
<span class="hljs-function">
    <span class="hljs-title">shiftCommentsForward</span> = <span class="hljs-params">(token, i, tokens)</span> -&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-41">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-41">&#x00a7;</a>
              </div>
              <p>Find the next surviving token and attach this token’s comments to it,
with a flag that we know to output such comments <em>before</em> that
token’s own compilation. (Otherwise comments are output following
the token they’re attached to.)</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      j = i
      j++ <span class="hljs-keyword">while</span> j <span class="hljs-keyword">isnt</span> tokens.length <span class="hljs-keyword">and</span> tokens[j][<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> DISCARDED
      <span class="hljs-keyword">unless</span> j <span class="hljs-keyword">is</span> tokens.length <span class="hljs-keyword">or</span> tokens[j][<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> DISCARDED
        comment.unshift = <span class="hljs-literal">yes</span> <span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> token.comments
        moveComments token, tokens[j]
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
      <span class="hljs-keyword">else</span> <span class="hljs-comment"># All following tokens are doomed!</span>
        j = tokens.length - <span class="hljs-number">1</span>
        insertPlaceholder token, j, tokens, <span class="hljs-string">&#x27;push&#x27;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-42">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-42">&#x00a7;</a>
              </div>
              <p>The generated tokens were added to the end, not inline, so we don’t skip.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
<span class="hljs-function">
    <span class="hljs-title">shiftCommentsBackward</span> = <span class="hljs-params">(token, i, tokens)</span> -&gt;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-43">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-43">&#x00a7;</a>
              </div>
              <p>Find the last surviving token and attach this token’s comments to it.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      j = i
      j-- <span class="hljs-keyword">while</span> j <span class="hljs-keyword">isnt</span> <span class="hljs-number">-1</span> <span class="hljs-keyword">and</span> tokens[j][<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> DISCARDED
      <span class="hljs-keyword">unless</span> j <span class="hljs-keyword">is</span> <span class="hljs-number">-1</span> <span class="hljs-keyword">or</span> tokens[j][<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> DISCARDED
        moveComments token, tokens[j]
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
      <span class="hljs-keyword">else</span> <span class="hljs-comment"># All previous tokens are doomed!</span>
        insertPlaceholder token, <span class="hljs-number">0</span>, tokens, <span class="hljs-string">&#x27;unshift&#x27;</span></pre></div></div>
            
        </li>
        
        
        <li id="section-44">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-44">&#x00a7;</a>
              </div>
              <p>We added two tokens, so shift forward to account for the insertion.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-keyword">return</span> <span class="hljs-number">3</span>

    @scanTokens (token, i, tokens) -&gt;
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span> <span class="hljs-keyword">unless</span> token.comments
      ret = <span class="hljs-number">1</span>
      <span class="hljs-keyword">if</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> DISCARDED</pre></div></div>
            
        </li>
        
        
        <li id="section-45">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-45">&#x00a7;</a>
              </div>
              <p>This token won’t survive passage through the parser, so we need to
rescue its attached tokens and redistribute them to nearby tokens.
Comments that don’t start a new line can shift backwards to the last
safe token, while other tokens should shift forward.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        dummyToken = comments: []
        j = token.comments.length - <span class="hljs-number">1</span>
        <span class="hljs-keyword">until</span> j <span class="hljs-keyword">is</span> <span class="hljs-number">-1</span>
          <span class="hljs-keyword">if</span> token.comments[j].newLine <span class="hljs-keyword">is</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">and</span> token.comments[j].here <span class="hljs-keyword">is</span> <span class="hljs-literal">no</span>
            dummyToken.comments.unshift token.comments[j]
            token.comments.splice j, <span class="hljs-number">1</span>
          j--
        <span class="hljs-keyword">if</span> dummyToken.comments.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
          ret = shiftCommentsBackward dummyToken, i - <span class="hljs-number">1</span>, tokens
        <span class="hljs-keyword">if</span> token.comments.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
          shiftCommentsForward token, i, tokens
      <span class="hljs-keyword">else</span> <span class="hljs-keyword">unless</span> dontShiftForward i, tokens</pre></div></div>
            
        </li>
        
        
        <li id="section-46">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-46">&#x00a7;</a>
              </div>
              <p>If any of this token’s comments start a line—there’s only
whitespace between the preceding newline and the start of the
comment—and this isn’t one of the special <code>JS</code> tokens, then
shift this comment forward to precede the next valid token.
<code>Block.compileComments</code> also has logic to make sure that
“starting new line” comments follow or precede the nearest
newline relative to the token that the comment is attached to,
but that newline might be inside a <code>}</code> or <code>)</code> or other generated
token that we really want this comment to output after. Therefore
we need to shift the comments here, avoiding such generated and
discarded tokens.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        dummyToken = comments: []
        j = token.comments.length - <span class="hljs-number">1</span>
        <span class="hljs-keyword">until</span> j <span class="hljs-keyword">is</span> <span class="hljs-number">-1</span>
          <span class="hljs-keyword">if</span> token.comments[j].newLine <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> token.comments[j].unshift <span class="hljs-keyword">and</span>
             <span class="hljs-keyword">not</span> (token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;JS&#x27;</span> <span class="hljs-keyword">and</span> token.generated)
            dummyToken.comments.unshift token.comments[j]
            token.comments.splice j, <span class="hljs-number">1</span>
          j--
        <span class="hljs-keyword">if</span> dummyToken.comments.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
          ret = shiftCommentsForward dummyToken, i + <span class="hljs-number">1</span>, tokens
      <span class="hljs-keyword">delete</span> token.comments <span class="hljs-keyword">if</span> token.comments?.length <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
      ret</pre></div></div>
            
        </li>
        
        
        <li id="section-47">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-47">&#x00a7;</a>
              </div>
              <p>Add location data to all tokens generated by the rewriter.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  addLocationDataToGeneratedTokens: <span class="hljs-function">-&gt;</span>
    @scanTokens (token, i, tokens) -&gt;
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span> <span class="hljs-keyword">if</span>     token[<span class="hljs-number">2</span>]
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span> <span class="hljs-keyword">unless</span> token.generated <span class="hljs-keyword">or</span> token.explicit
      <span class="hljs-keyword">if</span> token.fromThen <span class="hljs-keyword">and</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;INDENT&#x27;</span>
        token[<span class="hljs-number">2</span>] = token.origin[<span class="hljs-number">2</span>]
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
      <span class="hljs-keyword">if</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;{&#x27;</span> <span class="hljs-keyword">and</span> nextLocation=tokens[i + <span class="hljs-number">1</span>]?[<span class="hljs-number">2</span>]
        {first_line: line, first_column: column, range: [rangeIndex]} = nextLocation
      <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> prevLocation = tokens[i - <span class="hljs-number">1</span>]?[<span class="hljs-number">2</span>]
        {last_line: line, last_column: column, range: [, rangeIndex]} = prevLocation
        column += <span class="hljs-number">1</span>
      <span class="hljs-keyword">else</span>
        line = column = <span class="hljs-number">0</span>
        rangeIndex = <span class="hljs-number">0</span>
      token[<span class="hljs-number">2</span>] = {
        first_line:            line
        first_column:          column
        last_line:             line
        last_column:           column
        last_line_exclusive:   line
        last_column_exclusive: column
        range: [rangeIndex, rangeIndex]
      }
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-48">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-48">&#x00a7;</a>
              </div>
              <p><code>OUTDENT</code> tokens should always be positioned at the last character of the
previous token, so that AST nodes ending in an <code>OUTDENT</code> token end up with a
location corresponding to the last “real” token under the node.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  fixIndentationLocationData: <span class="hljs-function">-&gt;</span>
    @allComments ?= extractAllCommentTokens @tokens
<span class="hljs-function">    <span class="hljs-title">findPrecedingComment</span> = <span class="hljs-params">(token, {afterPosition, indentSize, first, indented})</span> =&gt;</span>
      tokenStart = token[<span class="hljs-number">2</span>].range[<span class="hljs-number">0</span>]
<span class="hljs-function">      <span class="hljs-title">matches</span> = <span class="hljs-params">(comment)</span> -&gt;</span>
        <span class="hljs-keyword">if</span> comment.outdented
          <span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> indentSize? <span class="hljs-keyword">and</span> comment.indentSize &gt; indentSize
        <span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">if</span> indented <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> comment.indented
        <span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> comment.locationData.range[<span class="hljs-number">0</span>] &lt; tokenStart
        <span class="hljs-keyword">return</span> <span class="hljs-literal">no</span> <span class="hljs-keyword">unless</span> comment.locationData.range[<span class="hljs-number">0</span>] &gt; afterPosition
        <span class="hljs-literal">yes</span>
      <span class="hljs-keyword">if</span> first
        lastMatching = <span class="hljs-literal">null</span>
        <span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> @allComments <span class="hljs-keyword">by</span> <span class="hljs-number">-1</span>
          <span class="hljs-keyword">if</span> matches comment
            lastMatching = comment
          <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> lastMatching
            <span class="hljs-keyword">return</span> lastMatching
        <span class="hljs-keyword">return</span> lastMatching
      <span class="hljs-keyword">for</span> comment <span class="hljs-keyword">in</span> @allComments <span class="hljs-keyword">when</span> matches comment <span class="hljs-keyword">by</span> <span class="hljs-number">-1</span>
        <span class="hljs-keyword">return</span> comment
      <span class="hljs-literal">null</span>

    @scanTokens (token, i, tokens) -&gt;
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span> <span class="hljs-keyword">unless</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;INDENT&#x27;</span>, <span class="hljs-string">&#x27;OUTDENT&#x27;</span>] <span class="hljs-keyword">or</span>
        (token.generated <span class="hljs-keyword">and</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;CALL_END&#x27;</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> token.data?.closingTagNameToken) <span class="hljs-keyword">or</span>
        (token.generated <span class="hljs-keyword">and</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;}&#x27;</span>)
      isIndent = token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;INDENT&#x27;</span>
      prevToken = token.prevToken ? tokens[i - <span class="hljs-number">1</span>]
      prevLocationData = prevToken[<span class="hljs-number">2</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-49">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-49">&#x00a7;</a>
              </div>
              <p>addLocationDataToGeneratedTokens() set the outdent’s location data
to the preceding token’s, but in order to detect comments inside an
empty “block” we want to look for comments preceding the next token.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      useNextToken = token.explicit <span class="hljs-keyword">or</span> token.generated
      <span class="hljs-keyword">if</span> useNextToken
        nextToken = token
        nextTokenIndex = i
        nextToken = tokens[nextTokenIndex++] <span class="hljs-keyword">while</span> (nextToken.explicit <span class="hljs-keyword">or</span> nextToken.generated) <span class="hljs-keyword">and</span> nextTokenIndex <span class="hljs-keyword">isnt</span> tokens.length - <span class="hljs-number">1</span>
      precedingComment = findPrecedingComment(
        <span class="hljs-keyword">if</span> useNextToken
          nextToken
        <span class="hljs-keyword">else</span>
          token
        afterPosition: prevLocationData.range[<span class="hljs-number">0</span>]
        indentSize: token.indentSize
        first: isIndent
        indented: useNextToken
      )
      <span class="hljs-keyword">if</span> isIndent
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span> <span class="hljs-keyword">unless</span> precedingComment?.newLine</pre></div></div>
            
        </li>
        
        
        <li id="section-50">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-50">&#x00a7;</a>
              </div>
              <p>We don’t want e.g. an implicit call at the end of an <code>if</code> condition to
include a following indented comment.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> token.generated <span class="hljs-keyword">and</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;CALL_END&#x27;</span> <span class="hljs-keyword">and</span> precedingComment?.indented
      prevLocationData = precedingComment.locationData <span class="hljs-keyword">if</span> precedingComment?
      token[<span class="hljs-number">2</span>] =
        first_line:
          <span class="hljs-keyword">if</span> precedingComment?
            prevLocationData.first_line
          <span class="hljs-keyword">else</span>
            prevLocationData.last_line
        first_column:
          <span class="hljs-keyword">if</span> precedingComment?
            <span class="hljs-keyword">if</span> isIndent
              <span class="hljs-number">0</span>
            <span class="hljs-keyword">else</span>
              prevLocationData.first_column
          <span class="hljs-keyword">else</span>
            prevLocationData.last_column
        last_line:              prevLocationData.last_line
        last_column:            prevLocationData.last_column
        last_line_exclusive:    prevLocationData.last_line_exclusive
        last_column_exclusive:  prevLocationData.last_column_exclusive
        range:
          <span class="hljs-keyword">if</span> isIndent <span class="hljs-keyword">and</span> precedingComment?
            [
              prevLocationData.range[<span class="hljs-number">0</span>] - precedingComment.indentSize
              prevLocationData.range[<span class="hljs-number">1</span>]
            ]
          <span class="hljs-keyword">else</span>
            prevLocationData.range
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-51">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-51">&#x00a7;</a>
              </div>
              <p>Because our grammar is LALR(1), it can’t handle some single-line
expressions that lack ending delimiters. The <strong>Rewriter</strong> adds the implicit
blocks, so it doesn’t need to. To keep the grammar clean and tidy, trailing
newlines within expressions are removed and the indentation tokens of empty
blocks are added.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  normalizeLines: <span class="hljs-function">-&gt;</span>
    starter = indent = outdent = <span class="hljs-literal">null</span>
    leading_switch_when = <span class="hljs-literal">null</span>
    leading_if_then = <span class="hljs-literal">null</span></pre></div></div>
            
        </li>
        
        
        <li id="section-52">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-52">&#x00a7;</a>
              </div>
              <p>Count <code>THEN</code> tags</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    ifThens = []
<span class="hljs-function">
    <span class="hljs-title">condition</span> = <span class="hljs-params">(token, i)</span> -&gt;</span>
      token[<span class="hljs-number">1</span>] <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;;&#x27;</span> <span class="hljs-keyword">and</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> SINGLE_CLOSERS <span class="hljs-keyword">and</span>
      <span class="hljs-keyword">not</span> (token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span> <span class="hljs-keyword">and</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">in</span> EXPRESSION_CLOSE) <span class="hljs-keyword">and</span>
      <span class="hljs-keyword">not</span> (token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;ELSE&#x27;</span> <span class="hljs-keyword">and</span>
           (starter <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;THEN&#x27;</span> <span class="hljs-keyword">or</span> (leading_if_then <span class="hljs-keyword">or</span> leading_switch_when))) <span class="hljs-keyword">and</span>
      <span class="hljs-keyword">not</span> (token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;CATCH&#x27;</span>, <span class="hljs-string">&#x27;FINALLY&#x27;</span>] <span class="hljs-keyword">and</span> starter <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;-&gt;&#x27;</span>, <span class="hljs-string">&#x27;=&gt;&#x27;</span>]) <span class="hljs-keyword">or</span>
      token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> CALL_CLOSERS <span class="hljs-keyword">and</span>
      (@tokens[i - <span class="hljs-number">1</span>].newLine <span class="hljs-keyword">or</span> @tokens[i - <span class="hljs-number">1</span>][<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;OUTDENT&#x27;</span>)
<span class="hljs-function">
    <span class="hljs-title">action</span> = <span class="hljs-params">(token, i)</span> -&gt;</span>
      ifThens.pop() <span class="hljs-keyword">if</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;ELSE&#x27;</span> <span class="hljs-keyword">and</span> starter <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;THEN&#x27;</span>
      @tokens.splice (<span class="hljs-keyword">if</span> @tag(i - <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;,&#x27;</span> <span class="hljs-keyword">then</span> i - <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> i), <span class="hljs-number">0</span>, outdent
<span class="hljs-function">
    <span class="hljs-title">closeElseTag</span> = <span class="hljs-params">(tokens, i)</span> =&gt;</span>
      tlen = ifThens.length
      <span class="hljs-keyword">return</span> i <span class="hljs-keyword">unless</span> tlen &gt; <span class="hljs-number">0</span>
      lastThen = ifThens.pop()
      [, outdentElse] = @indentation tokens[lastThen]</pre></div></div>
            
        </li>
        
        
        <li id="section-53">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-53">&#x00a7;</a>
              </div>
              <p>Insert <code>OUTDENT</code> to close inner <code>IF</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      outdentElse[<span class="hljs-number">1</span>] = tlen*<span class="hljs-number">2</span>
      tokens.splice(i, <span class="hljs-number">0</span>, outdentElse)</pre></div></div>
            
        </li>
        
        
        <li id="section-54">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-54">&#x00a7;</a>
              </div>
              <p>Insert <code>OUTDENT</code> to close outer <code>IF</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      outdentElse[<span class="hljs-number">1</span>] = <span class="hljs-number">2</span>
      tokens.splice(i + <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, outdentElse)</pre></div></div>
            
        </li>
        
        
        <li id="section-55">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-55">&#x00a7;</a>
              </div>
              <p>Remove outdents from the end.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>      @detectEnd i + <span class="hljs-number">2</span>,
        <span class="hljs-function"><span class="hljs-params">(token, i)</span> -&gt;</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;OUTDENT&#x27;</span>, <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>]
        (token, i) -&gt;
            <span class="hljs-keyword">if</span> @tag(i) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;OUTDENT&#x27;</span> <span class="hljs-keyword">and</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;OUTDENT&#x27;</span>
              tokens.splice i, <span class="hljs-number">2</span>
      i + <span class="hljs-number">2</span>

    @scanTokens (token, i, tokens) -&gt;
      [tag] = token
      conditionTag = tag <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;-&gt;&#x27;</span>, <span class="hljs-string">&#x27;=&gt;&#x27;</span>] <span class="hljs-keyword">and</span>
        @findTagsBackwards(i, [<span class="hljs-string">&#x27;IF&#x27;</span>, <span class="hljs-string">&#x27;WHILE&#x27;</span>, <span class="hljs-string">&#x27;FOR&#x27;</span>, <span class="hljs-string">&#x27;UNTIL&#x27;</span>, <span class="hljs-string">&#x27;SWITCH&#x27;</span>, <span class="hljs-string">&#x27;WHEN&#x27;</span>, <span class="hljs-string">&#x27;LEADING_WHEN&#x27;</span>, <span class="hljs-string">&#x27;[&#x27;</span>, <span class="hljs-string">&#x27;INDEX_START&#x27;</span>]) <span class="hljs-keyword">and</span>
        <span class="hljs-keyword">not</span> (@findTagsBackwards i, [<span class="hljs-string">&#x27;THEN&#x27;</span>, <span class="hljs-string">&#x27;..&#x27;</span>, <span class="hljs-string">&#x27;...&#x27;</span>])

      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>
        <span class="hljs-keyword">if</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;ELSE&#x27;</span> <span class="hljs-keyword">and</span> @tag(i - <span class="hljs-number">1</span>) <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;OUTDENT&#x27;</span>
          tokens.splice i, <span class="hljs-number">1</span>, @indentation()...
          <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
        <span class="hljs-keyword">if</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">in</span> EXPRESSION_CLOSE
          <span class="hljs-keyword">if</span> token[<span class="hljs-number">1</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;;&#x27;</span> <span class="hljs-keyword">and</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;OUTDENT&#x27;</span>
            tokens[i + <span class="hljs-number">1</span>].prevToken = token
            moveComments token, tokens[i + <span class="hljs-number">1</span>]
          tokens.splice i, <span class="hljs-number">1</span>
          <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;CATCH&#x27;</span>
        <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> [<span class="hljs-number">1.</span><span class="hljs-number">.2</span>] <span class="hljs-keyword">when</span> @tag(i + j) <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;OUTDENT&#x27;</span>, <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>, <span class="hljs-string">&#x27;FINALLY&#x27;</span>]
          tokens.splice i + j, <span class="hljs-number">0</span>, @indentation()...
          <span class="hljs-keyword">return</span> <span class="hljs-number">2</span> + j
      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;-&gt;&#x27;</span>, <span class="hljs-string">&#x27;=&gt;&#x27;</span>] <span class="hljs-keyword">and</span> (@tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;,&#x27;</span>, <span class="hljs-string">&#x27;]&#x27;</span>] <span class="hljs-keyword">or</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;.&#x27;</span> <span class="hljs-keyword">and</span> token.newLine)
        [indent, outdent] = @indentation tokens[i]
        tokens.splice i + <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, indent, outdent
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
      <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">in</span> SINGLE_LINERS <span class="hljs-keyword">and</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;INDENT&#x27;</span> <span class="hljs-keyword">and</span>
         <span class="hljs-keyword">not</span> (tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;ELSE&#x27;</span> <span class="hljs-keyword">and</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;IF&#x27;</span>) <span class="hljs-keyword">and</span>
         <span class="hljs-keyword">not</span> conditionTag
        starter = tag
        [indent, outdent] = @indentation tokens[i]
        indent.fromThen   = <span class="hljs-literal">true</span> <span class="hljs-keyword">if</span> starter <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;THEN&#x27;</span>
        <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;THEN&#x27;</span>
          leading_switch_when = @findTagsBackwards(i, [<span class="hljs-string">&#x27;LEADING_WHEN&#x27;</span>]) <span class="hljs-keyword">and</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;IF&#x27;</span>
          leading_if_then = @findTagsBackwards(i, [<span class="hljs-string">&#x27;IF&#x27;</span>]) <span class="hljs-keyword">and</span> @tag(i + <span class="hljs-number">1</span>) <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;IF&#x27;</span>
        ifThens.push i <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;THEN&#x27;</span> <span class="hljs-keyword">and</span> @findTagsBackwards(i, [<span class="hljs-string">&#x27;IF&#x27;</span>])</pre></div></div>
            
        </li>
        
        
        <li id="section-56">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-56">&#x00a7;</a>
              </div>
              <p><code>ELSE</code> tag is not closed.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;ELSE&#x27;</span> <span class="hljs-keyword">and</span> @tag(i - <span class="hljs-number">1</span>) <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;OUTDENT&#x27;</span>
          i = closeElseTag tokens, i
        tokens.splice i + <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, indent
        @detectEnd i + <span class="hljs-number">2</span>, condition, action
        tokens.splice i, <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;THEN&#x27;</span>
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-57">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-57">&#x00a7;</a>
              </div>
              <p>Tag postfix conditionals as such, so that we can parse them with a
different precedence.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  tagPostfixConditionals: <span class="hljs-function">-&gt;</span>
    original = <span class="hljs-literal">null</span>
<span class="hljs-function">
    <span class="hljs-title">condition</span> = <span class="hljs-params">(token, i)</span> -&gt;</span>
      [tag] = token
      [prevTag] = @tokens[i - <span class="hljs-number">1</span>]
      tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;TERMINATOR&#x27;</span> <span class="hljs-keyword">or</span> (tag <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;INDENT&#x27;</span> <span class="hljs-keyword">and</span> prevTag <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> SINGLE_LINERS)
<span class="hljs-function">
    <span class="hljs-title">action</span> = <span class="hljs-params">(token, i)</span> -&gt;</span>
      <span class="hljs-keyword">if</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">isnt</span> <span class="hljs-string">&#x27;INDENT&#x27;</span> <span class="hljs-keyword">or</span> (token.generated <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> token.fromThen)
        original[<span class="hljs-number">0</span>] = <span class="hljs-string">&#x27;POST_&#x27;</span> + original[<span class="hljs-number">0</span>]

    @scanTokens (token, i) -&gt;
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span> <span class="hljs-keyword">unless</span> token[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-string">&#x27;IF&#x27;</span>
      original = token
      @detectEnd i + <span class="hljs-number">1</span>, condition, action
      <span class="hljs-keyword">return</span> <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-58">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-58">&#x00a7;</a>
              </div>
              <p>For tokens with extra data, we want to make that data visible to the grammar
by wrapping the token value as a String() object and setting the data as
properties of that object. The grammar should then be responsible for
cleaning this up for the node constructor: unwrapping the token value to a
primitive string and separately passing any expected token data properties</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  exposeTokenDataToGrammar: <span class="hljs-function">-&gt;</span>
    @scanTokens (token, i) -&gt;
      <span class="hljs-keyword">if</span> token.generated <span class="hljs-keyword">or</span> (token.data <span class="hljs-keyword">and</span> <span class="hljs-built_in">Object</span>.keys(token.data).length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>)
        token[<span class="hljs-number">1</span>] = <span class="hljs-keyword">new</span> <span class="hljs-built_in">String</span> token[<span class="hljs-number">1</span>]
        token[<span class="hljs-number">1</span>][key] = val <span class="hljs-keyword">for</span> own key, val <span class="hljs-keyword">of</span> (token.data ? {})
        token[<span class="hljs-number">1</span>].generated = <span class="hljs-literal">yes</span> <span class="hljs-keyword">if</span> token.generated
      <span class="hljs-number">1</span></pre></div></div>
            
        </li>
        
        
        <li id="section-59">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-59">&#x00a7;</a>
              </div>
              <p>Generate the indentation tokens, based on another token on the same line.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  indentation: <span class="hljs-function"><span class="hljs-params">(origin)</span> -&gt;</span>
    indent  = [<span class="hljs-string">&#x27;INDENT&#x27;</span>, <span class="hljs-number">2</span>]
    outdent = [<span class="hljs-string">&#x27;OUTDENT&#x27;</span>, <span class="hljs-number">2</span>]
    <span class="hljs-keyword">if</span> origin
      indent.generated = outdent.generated = <span class="hljs-literal">yes</span>
      indent.origin = outdent.origin = origin
    <span class="hljs-keyword">else</span>
      indent.explicit = outdent.explicit = <span class="hljs-literal">yes</span>
    [indent, outdent]

  generate: generate</pre></div></div>
            
        </li>
        
        
        <li id="section-60">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-60">&#x00a7;</a>
              </div>
              <p>Look up a tag by token index.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>  tag: <span class="hljs-function"><span class="hljs-params">(i)</span> -&gt;</span> @tokens[i]?[<span class="hljs-number">0</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-61">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-61">&#x00a7;</a>
              </div>
              <h2 id="constants">Constants</h2>

            </div>
            
        </li>
        
        
        <li id="section-62">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-62">&#x00a7;</a>
              </div>
              
            </div>
            
        </li>
        
        
        <li id="section-63">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-63">&#x00a7;</a>
              </div>
              <p>List of the token pairs that must be balanced.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>BALANCED_PAIRS = [
  [<span class="hljs-string">&#x27;(&#x27;</span>, <span class="hljs-string">&#x27;)&#x27;</span>]
  [<span class="hljs-string">&#x27;[&#x27;</span>, <span class="hljs-string">&#x27;]&#x27;</span>]
  [<span class="hljs-string">&#x27;{&#x27;</span>, <span class="hljs-string">&#x27;}&#x27;</span>]
  [<span class="hljs-string">&#x27;INDENT&#x27;</span>, <span class="hljs-string">&#x27;OUTDENT&#x27;</span>],
  [<span class="hljs-string">&#x27;CALL_START&#x27;</span>, <span class="hljs-string">&#x27;CALL_END&#x27;</span>]
  [<span class="hljs-string">&#x27;PARAM_START&#x27;</span>, <span class="hljs-string">&#x27;PARAM_END&#x27;</span>]
  [<span class="hljs-string">&#x27;INDEX_START&#x27;</span>, <span class="hljs-string">&#x27;INDEX_END&#x27;</span>]
  [<span class="hljs-string">&#x27;STRING_START&#x27;</span>, <span class="hljs-string">&#x27;STRING_END&#x27;</span>]
  [<span class="hljs-string">&#x27;INTERPOLATION_START&#x27;</span>, <span class="hljs-string">&#x27;INTERPOLATION_END&#x27;</span>]
  [<span class="hljs-string">&#x27;REGEX_START&#x27;</span>, <span class="hljs-string">&#x27;REGEX_END&#x27;</span>]
]</pre></div></div>
            
        </li>
        
        
        <li id="section-64">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-64">&#x00a7;</a>
              </div>
              <p>The inverse mappings of <code>BALANCED_PAIRS</code> we’re trying to fix up, so we can
look things up from either end.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.INVERSES = INVERSES = {}</pre></div></div>
            
        </li>
        
        
        <li id="section-65">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-65">&#x00a7;</a>
              </div>
              <p>The tokens that signal the start/end of a balanced pair.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>EXPRESSION_START = []
EXPRESSION_END   = []

<span class="hljs-keyword">for</span> [left, right] <span class="hljs-keyword">in</span> BALANCED_PAIRS
  EXPRESSION_START.push INVERSES[right] = left
  EXPRESSION_END  .push INVERSES[left] = right</pre></div></div>
            
        </li>
        
        
        <li id="section-66">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-66">&#x00a7;</a>
              </div>
              <p>Tokens that indicate the close of a clause of an expression.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>EXPRESSION_CLOSE = [<span class="hljs-string">&#x27;CATCH&#x27;</span>, <span class="hljs-string">&#x27;THEN&#x27;</span>, <span class="hljs-string">&#x27;ELSE&#x27;</span>, <span class="hljs-string">&#x27;FINALLY&#x27;</span>].concat EXPRESSION_END</pre></div></div>
            
        </li>
        
        
        <li id="section-67">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-67">&#x00a7;</a>
              </div>
              <p>Tokens that, if followed by an <code>IMPLICIT_CALL</code>, indicate a function invocation.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>IMPLICIT_FUNC    = [<span class="hljs-string">&#x27;IDENTIFIER&#x27;</span>, <span class="hljs-string">&#x27;PROPERTY&#x27;</span>, <span class="hljs-string">&#x27;SUPER&#x27;</span>, <span class="hljs-string">&#x27;)&#x27;</span>, <span class="hljs-string">&#x27;CALL_END&#x27;</span>, <span class="hljs-string">&#x27;]&#x27;</span>, <span class="hljs-string">&#x27;INDEX_END&#x27;</span>, <span class="hljs-string">&#x27;@&#x27;</span>, <span class="hljs-string">&#x27;THIS&#x27;</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-68">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-68">&#x00a7;</a>
              </div>
              <p>If preceded by an <code>IMPLICIT_FUNC</code>, indicates a function invocation.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>IMPLICIT_CALL    = [
  <span class="hljs-string">&#x27;IDENTIFIER&#x27;</span>, <span class="hljs-string">&#x27;JSX_TAG&#x27;</span>, <span class="hljs-string">&#x27;PROPERTY&#x27;</span>, <span class="hljs-string">&#x27;NUMBER&#x27;</span>, <span class="hljs-string">&#x27;INFINITY&#x27;</span>, <span class="hljs-string">&#x27;NAN&#x27;</span>
  <span class="hljs-string">&#x27;STRING&#x27;</span>, <span class="hljs-string">&#x27;STRING_START&#x27;</span>, <span class="hljs-string">&#x27;REGEX&#x27;</span>, <span class="hljs-string">&#x27;REGEX_START&#x27;</span>, <span class="hljs-string">&#x27;JS&#x27;</span>
  <span class="hljs-string">&#x27;NEW&#x27;</span>, <span class="hljs-string">&#x27;PARAM_START&#x27;</span>, <span class="hljs-string">&#x27;CLASS&#x27;</span>, <span class="hljs-string">&#x27;IF&#x27;</span>, <span class="hljs-string">&#x27;TRY&#x27;</span>, <span class="hljs-string">&#x27;SWITCH&#x27;</span>, <span class="hljs-string">&#x27;THIS&#x27;</span>
  <span class="hljs-string">&#x27;DYNAMIC_IMPORT&#x27;</span>, <span class="hljs-string">&#x27;IMPORT_META&#x27;</span>, <span class="hljs-string">&#x27;NEW_TARGET&#x27;</span>
  <span class="hljs-string">&#x27;UNDEFINED&#x27;</span>, <span class="hljs-string">&#x27;NULL&#x27;</span>, <span class="hljs-string">&#x27;BOOL&#x27;</span>
  <span class="hljs-string">&#x27;UNARY&#x27;</span>, <span class="hljs-string">&#x27;DO&#x27;</span>, <span class="hljs-string">&#x27;DO_IIFE&#x27;</span>, <span class="hljs-string">&#x27;YIELD&#x27;</span>, <span class="hljs-string">&#x27;AWAIT&#x27;</span>, <span class="hljs-string">&#x27;UNARY_MATH&#x27;</span>, <span class="hljs-string">&#x27;SUPER&#x27;</span>, <span class="hljs-string">&#x27;THROW&#x27;</span>
  <span class="hljs-string">&#x27;@&#x27;</span>, <span class="hljs-string">&#x27;-&gt;&#x27;</span>, <span class="hljs-string">&#x27;=&gt;&#x27;</span>, <span class="hljs-string">&#x27;[&#x27;</span>, <span class="hljs-string">&#x27;(&#x27;</span>, <span class="hljs-string">&#x27;{&#x27;</span>, <span class="hljs-string">&#x27;--&#x27;</span>, <span class="hljs-string">&#x27;++&#x27;</span>
]

IMPLICIT_UNSPACED_CALL = [<span class="hljs-string">&#x27;+&#x27;</span>, <span class="hljs-string">&#x27;-&#x27;</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-69">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-69">&#x00a7;</a>
              </div>
              <p>Tokens that always mark the end of an implicit call for single-liners.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>IMPLICIT_END     = [<span class="hljs-string">&#x27;POST_IF&#x27;</span>, <span class="hljs-string">&#x27;FOR&#x27;</span>, <span class="hljs-string">&#x27;WHILE&#x27;</span>, <span class="hljs-string">&#x27;UNTIL&#x27;</span>, <span class="hljs-string">&#x27;WHEN&#x27;</span>, <span class="hljs-string">&#x27;BY&#x27;</span>,
  <span class="hljs-string">&#x27;LOOP&#x27;</span>, <span class="hljs-string">&#x27;TERMINATOR&#x27;</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-70">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-70">&#x00a7;</a>
              </div>
              <p>Single-line flavors of block expressions that have unclosed endings.
The grammar can’t disambiguate them, so we insert the implicit indentation.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>SINGLE_LINERS    = [<span class="hljs-string">&#x27;ELSE&#x27;</span>, <span class="hljs-string">&#x27;-&gt;&#x27;</span>, <span class="hljs-string">&#x27;=&gt;&#x27;</span>, <span class="hljs-string">&#x27;TRY&#x27;</span>, <span class="hljs-string">&#x27;FINALLY&#x27;</span>, <span class="hljs-string">&#x27;THEN&#x27;</span>]
SINGLE_CLOSERS   = [<span class="hljs-string">&#x27;TERMINATOR&#x27;</span>, <span class="hljs-string">&#x27;CATCH&#x27;</span>, <span class="hljs-string">&#x27;FINALLY&#x27;</span>, <span class="hljs-string">&#x27;ELSE&#x27;</span>, <span class="hljs-string">&#x27;OUTDENT&#x27;</span>, <span class="hljs-string">&#x27;LEADING_WHEN&#x27;</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-71">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-71">&#x00a7;</a>
              </div>
              <p>Tokens that end a line.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>LINEBREAKS       = [<span class="hljs-string">&#x27;TERMINATOR&#x27;</span>, <span class="hljs-string">&#x27;INDENT&#x27;</span>, <span class="hljs-string">&#x27;OUTDENT&#x27;</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-72">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-72">&#x00a7;</a>
              </div>
              <p>Tokens that close open calls when they follow a newline.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>CALL_CLOSERS     = [<span class="hljs-string">&#x27;.&#x27;</span>, <span class="hljs-string">&#x27;?.&#x27;</span>, <span class="hljs-string">&#x27;::&#x27;</span>, <span class="hljs-string">&#x27;?::&#x27;</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-73">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-73">&#x00a7;</a>
              </div>
              <p>Tokens that prevent a subsequent indent from ending implicit calls/objects</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>CONTROL_IN_IMPLICIT = [<span class="hljs-string">&#x27;IF&#x27;</span>, <span class="hljs-string">&#x27;TRY&#x27;</span>, <span class="hljs-string">&#x27;FINALLY&#x27;</span>, <span class="hljs-string">&#x27;CATCH&#x27;</span>, <span class="hljs-string">&#x27;CLASS&#x27;</span>, <span class="hljs-string">&#x27;SWITCH&#x27;</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-74">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-74">&#x00a7;</a>
              </div>
              <p>Tokens that are swallowed up by the parser, never leading to code generation.
You can spot these in <code>grammar.coffee</code> because the <code>o</code> function second
argument doesn’t contain a <code>new</code> call for these tokens.
<code>STRING_START</code> isn’t on this list because its <code>locationData</code> matches that of
the node that becomes <code>StringWithInterpolations</code>, and therefore
<code>addDataToNode</code> attaches <code>STRING_START</code>’s tokens to that node.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>DISCARDED = [<span class="hljs-string">&#x27;(&#x27;</span>, <span class="hljs-string">&#x27;)&#x27;</span>, <span class="hljs-string">&#x27;[&#x27;</span>, <span class="hljs-string">&#x27;]&#x27;</span>, <span class="hljs-string">&#x27;{&#x27;</span>, <span class="hljs-string">&#x27;}&#x27;</span>, <span class="hljs-string">&#x27;:&#x27;</span>, <span class="hljs-string">&#x27;.&#x27;</span>, <span class="hljs-string">&#x27;..&#x27;</span>, <span class="hljs-string">&#x27;...&#x27;</span>, <span class="hljs-string">&#x27;,&#x27;</span>, <span class="hljs-string">&#x27;=&#x27;</span>, <span class="hljs-string">&#x27;++&#x27;</span>, <span class="hljs-string">&#x27;--&#x27;</span>, <span class="hljs-string">&#x27;?&#x27;</span>,
  <span class="hljs-string">&#x27;AS&#x27;</span>, <span class="hljs-string">&#x27;AWAIT&#x27;</span>, <span class="hljs-string">&#x27;CALL_START&#x27;</span>, <span class="hljs-string">&#x27;CALL_END&#x27;</span>, <span class="hljs-string">&#x27;DEFAULT&#x27;</span>, <span class="hljs-string">&#x27;DO&#x27;</span>, <span class="hljs-string">&#x27;DO_IIFE&#x27;</span>, <span class="hljs-string">&#x27;ELSE&#x27;</span>,
  <span class="hljs-string">&#x27;EXTENDS&#x27;</span>, <span class="hljs-string">&#x27;EXPORT&#x27;</span>, <span class="hljs-string">&#x27;FORIN&#x27;</span>, <span class="hljs-string">&#x27;FOROF&#x27;</span>, <span class="hljs-string">&#x27;FORFROM&#x27;</span>, <span class="hljs-string">&#x27;IMPORT&#x27;</span>, <span class="hljs-string">&#x27;INDENT&#x27;</span>, <span class="hljs-string">&#x27;INDEX_SOAK&#x27;</span>,
  <span class="hljs-string">&#x27;INTERPOLATION_START&#x27;</span>, <span class="hljs-string">&#x27;INTERPOLATION_END&#x27;</span>, <span class="hljs-string">&#x27;LEADING_WHEN&#x27;</span>, <span class="hljs-string">&#x27;OUTDENT&#x27;</span>, <span class="hljs-string">&#x27;PARAM_END&#x27;</span>,
  <span class="hljs-string">&#x27;REGEX_START&#x27;</span>, <span class="hljs-string">&#x27;REGEX_END&#x27;</span>, <span class="hljs-string">&#x27;RETURN&#x27;</span>, <span class="hljs-string">&#x27;STRING_END&#x27;</span>, <span class="hljs-string">&#x27;THROW&#x27;</span>, <span class="hljs-string">&#x27;UNARY&#x27;</span>, <span class="hljs-string">&#x27;YIELD&#x27;</span>
].concat IMPLICIT_UNSPACED_CALL.concat IMPLICIT_END.concat CALL_CLOSERS.concat CONTROL_IN_IMPLICIT</pre></div></div>
            
        </li>
        
        
        <li id="section-75">
            <div class="annotation">
              
              <div class="sswrap ">
                <a class="ss" href="#section-75">&#x00a7;</a>
              </div>
              <p>Tokens that, when appearing at the end of a line, suppress a following TERMINATOR/INDENT token</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="hljs-built_in">exports</span>.UNFINISHED = UNFINISHED = [<span class="hljs-string">&#x27;\\&#x27;</span>, <span class="hljs-string">&#x27;.&#x27;</span>, <span class="hljs-string">&#x27;?.&#x27;</span>, <span class="hljs-string">&#x27;?::&#x27;</span>, <span class="hljs-string">&#x27;UNARY&#x27;</span>, <span class="hljs-string">&#x27;DO&#x27;</span>, <span class="hljs-string">&#x27;DO_IIFE&#x27;</span>, <span class="hljs-string">&#x27;MATH&#x27;</span>, <span class="hljs-string">&#x27;UNARY_MATH&#x27;</span>, <span class="hljs-string">&#x27;+&#x27;</span>, <span class="hljs-string">&#x27;-&#x27;</span>,
           <span class="hljs-string">&#x27;**&#x27;</span>, <span class="hljs-string">&#x27;SHIFT&#x27;</span>, <span class="hljs-string">&#x27;RELATION&#x27;</span>, <span class="hljs-string">&#x27;COMPARE&#x27;</span>, <span class="hljs-string">&#x27;&amp;&#x27;</span>, <span class="hljs-string">&#x27;^&#x27;</span>, <span class="hljs-string">&#x27;|&#x27;</span>, <span class="hljs-string">&#x27;&amp;&amp;&#x27;</span>, <span class="hljs-string">&#x27;||&#x27;</span>,
           <span class="hljs-string">&#x27;BIN?&#x27;</span>, <span class="hljs-string">&#x27;EXTENDS&#x27;</span>]</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
